﻿/******************CRM.TiemNang.cs********************\
 * Module name: Logic Lien He
 * Description: Business logic for Lien He
 * Author: cyworld
\****************************************************/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Windows.Controls.Ribbon;
using OODCRM.Dialogs;
using OODCRM.Common;
using System.Windows;

namespace OODCRM
{
    public partial class MainWindow : RibbonWindow
    {

        internal void TiemNang_NewExecuted()
        {
            dicDialogData[CRMDialog.ThemTiemNang] = new TIEMNANG()
            {
                DOITUONG = new DOITUONG()
                {
                    IdDoiTuong = System.Guid.NewGuid(),
                    ThoiDiemTao = DateTime.Now,
                    ThoiDiemSua = DateTime.Now,
                    DungChung = true,
                    LOAIDOITUONG1 = dataContext.LOAIDOITUONGs.FirstOrDefault(),
                    User = dataContext.Users.FirstOrDefault(),
                    User1 = dataContext.Users.FirstOrDefault(),
                    User2 = dataContext.Users.FirstOrDefault(),
                },
                DIACHI = new DIACHI(),
                DIACHI1 = new DIACHI(),
            };

            dicDialogMode[CRMDialog.ThemTiemNang] = DialogMode.Them;

            EnableDialog(CRMDialog.ThemTiemNang);
        }

        internal void TiemNang_EditExecuted()
        {
            TIEMNANG item = dgrTiemNang.SelectedItem as TIEMNANG;
            if (item == null) return;

            dicDialogData[CRMDialog.ThemTiemNang] = item;

            dicDialogMode[CRMDialog.ThemTiemNang] = DialogMode.Sua;

            EnableDialog(CRMDialog.ThemTiemNang);
        }

        internal void TiemNang_DeleteExecuted()
        {
            TIEMNANG item = dgrTiemNang.SelectedItem as TIEMNANG;
            if (item == null) return;

            // Check FK before delete
            var x1 = dataContext.CHITIETPHANLOAITIEMNANGs.FirstOrDefault(x => x.MaTiemNang == item.MaHieu) as CHITIETPHANLOAITIEMNANG;
            if (x1 != null)
            {
                MessageBox.Show("Tồn tại thông tin  chi tiết liên quan tới tiềm năng này! (" + x1.TenPhanLoai + ")",
                    "Không thể xoá", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }          


             //Confirm and delete
            if (MessageBox.Show(
                    "Bạn có chắc muốn xoá người liên hệ \"" + item.HoVaTen + "\" ?", "Xác nhận xoá",
                    MessageBoxButton.YesNo,
                    MessageBoxImage.Question
                    )
                    == MessageBoxResult.Yes)
            {
                listTiemNang.Remove(item);
                dataContext.TIEMNANGs.DeleteOnSubmit(item);
                try
                {
                    dataContext.SubmitChanges();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }
}
